import java.util.Vector;

import edu.smu.tspell.wordnet.*;


public class WordNet {
	
	/**
	 * Return a list of synonyms of the word
	 * @param word
	 * @return synonyms of the word
	 */
	public static Vector<String> getSynonyms(String word)
	{
		System.setProperty("wordnet.database.dir", "C:\\WordNet\\dict\\");
		
		Vector<String> synonyms = new Vector<String>();
		
		WordNetDatabase database = WordNetDatabase.getFileInstance();

		Synset[] synsets = database.getSynsets(word);
		
		//If the word is not in wordnet, return an empty list!
		if(synsets.length==0)return synonyms;
		
		int maxCount=0;
		Synset bestSynset=synsets[0];
		for(int a=0;a<synsets.length;a++)
		{
			try{
				if(synsets[a].getTagCount(word)>maxCount)
				{
					maxCount=synsets[a].getTagCount(word);
					bestSynset = synsets[a];
				}
			}
			catch(WordNetException e)
			{}
		}

		if(bestSynset.getType()==SynsetType.NOUN)
		{
			NounSynset synset = (NounSynset)bestSynset;
			//Get the synonyms
			String[] syn = synset.getWordForms();
			for(String s : syn)
			{
				if(!s.equals(word) && !synonyms.contains(s))synonyms.add(s);
			}
		}
		else if(bestSynset.getType()==SynsetType.VERB)
		{
			VerbSynset synset = (VerbSynset)bestSynset;
			//Get the synonyms
			String[] str = synset.getWordForms();
			for(String s : str)
			{
				if(!s.equals(word) && !synonyms.contains(s))synonyms.add(s);
			}
		}
		else if(bestSynset.getType()==SynsetType.ADJECTIVE)
		{
			AdjectiveSynset synset = (AdjectiveSynset)bestSynset;
			//Get synonyms
			String[] syn = synset.getWordForms();
			for(String s : syn)
			{
				if(!s.equals(word) && !synonyms.contains(s))synonyms.add(s);
			}
			//Get the RELATED WORDS
			AdjectiveSynset[] related = synset.getRelated();
			for(int i=0;i<related.length;i++)
			{
				String[] str = related[i].getWordForms();
				for(String s : str)
				{
					if(!s.equals(word) && !synonyms.contains(s))synonyms.add(s);
				}
			}
//			//GET THE SIMILAR WORDS
//			AdjectiveSynset[] similars = synset.getSimilar();
//			for(int i=0;i<similars.length;i++)
//			{
//				String[] str = similars[i].getWordForms();
//				for(String s : str)
//				{
//					if(!s.equals(word) && !synonyms.contains(s))synonyms.add(s);
//				}
//			}
		}
		else if(bestSynset.getType()==SynsetType.ADVERB)
		{
			AdverbSynset synset = (AdverbSynset)bestSynset;
			//Get the synonyms
			String[] syn = synset.getWordForms();
			for(String s : syn)
			{
				if(!s.equals(word) && !synonyms.contains(s))synonyms.add(s);
			}
		}
		else if(bestSynset.getType()==SynsetType.ADJECTIVE_SATELLITE)
		{
			AdjectiveSatelliteSynset synsetSat = (AdjectiveSatelliteSynset)bestSynset;
			AdjectiveSynset synset = (AdjectiveSynset)synsetSat.getHeadSynset();
			//Get synonyms
			String[] syn = synset.getWordForms();
			for(String s : syn)
			{
				if(!s.equals(word) && !synonyms.contains(s))synonyms.add(s);
			}
			//Get the RELATED WORDS
			AdjectiveSynset[] related = synset.getRelated();
			for(int i=0;i<related.length;i++)
			{
				String[] str = related[i].getWordForms();
				for(String s : str)
				{
					if(!s.equals(word) && !synonyms.contains(s))synonyms.add(s);
				}
			}
			//GET THE SIMILAR WORDS
			AdjectiveSynset[] similars = synset.getSimilar();
			for(int i=0;i<similars.length;i++)
			{
				String[] str = similars[i].getWordForms();
				for(String s : str)
				{
					if(!s.equals(word) && !synonyms.contains(s))synonyms.add(s);
				}
			}
		}
		
		return synonyms;
	}
	
	
	
	
	/**
	 * Return a list of antonyms of a word
	 * @param word
	 * @return list of antonyms of the word
	 */
	public static Vector<String> getAntonyms(String word)
	{
		System.setProperty("wordnet.database.dir", "C:\\WordNet\\dict\\");
		
		Vector<String> antonyms = new Vector<String>();
		
		WordNetDatabase database = WordNetDatabase.getFileInstance();

		Synset[] synsets = database.getSynsets(word);
		
		//If the word is not in wordnet, return an empty list!
		if(synsets.length==0)return antonyms;
		
		int maxCount=0;
		Synset bestSynset=synsets[0];
		for(int a=0;a<synsets.length;a++)
		{
			try{
				if(synsets[a].getTagCount(word)>maxCount)
				{
					maxCount=synsets[a].getTagCount(word);
					bestSynset = synsets[a];
				}
			}
			catch(WordNetException e)
			{}
		}
		
		//If it's an adjective satellite, take the head adjective
		if(bestSynset.getType()==SynsetType.ADJECTIVE_SATELLITE)
		{
			AdjectiveSatelliteSynset synsetSat = (AdjectiveSatelliteSynset)bestSynset;
			AdjectiveSynset synset = (AdjectiveSynset)synsetSat.getHeadSynset();
			bestSynset = synset;
			word = bestSynset.getWordForms()[0];
		}
		
		//Get the antonyms
		WordSense[] ants = bestSynset.getAntonyms(word);
		for(int i=0;i<ants.length;i++)
		{
			WordSense ant = ants[i];
			//antonyms.add(ant.getWordForm());
			antonyms = getSynonyms(ant.getWordForm());
			if(!antonyms.contains(ant.getWordForm()))antonyms.add(0,ant.getWordForm());
		}
		
		
		return antonyms;
	}
	
	
	
	
	public static void main(String[] args){
		
		String word = new String("best");
		System.out.println(word);
		
		Vector<String> syn = getSynonyms(word);
		System.out.println("Synonyms: "+syn);
		
		Vector<String> ant = getAntonyms(word);
		System.out.println("Antonyms: "+ant);
	}
}
